using System;
using System.Collections.Generic;

namespace zTalk.Network
{
    public class SocketWorker : SocketBase
    {
        public static List<SocketWorker> Instances 
            = new List<SocketWorker>();

        public SocketWorker()
        {
            Instances.Add(this);
        }

        public void Start()
        {
            try
            {
                startReceive();
            }
            catch (ObjectDisposedException)
            {
                Close();
            }
        }

        public override void Close()
        {
            string clientHost = "";

            try
            {
                clientHost = Socket.RemoteEndPoint.ToString();
            }
            catch (Exception)
            {
                return;
            }
            finally
            {
                Instances.Remove(this);
                base.Close();
            }

            Console.WriteLine(
                "Disconnected {0} ({1} online)",
                clientHost, Instances.Count
            );
        }
    }
}
